﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Dao;
using Core.Entity;
using System.Data;

namespace Core.Business
{
    public class OrderBus
    {
        private OrderDao dao;
        public OrderBus()
        {
            dao = new OrderDao();
        }

        public bool insert(Order order) {
            return dao.insert(order);
        }

        public bool update(Order order) {
            return dao.update(order);
        }

        public bool delete(int id) {
            return dao.delete(id);
        }

        public bool delete(String ids) {
            return dao.delete(ids);
        }

        public Order get(int id) {
            DataTable dt = dao.get(id);
            Order order = new Order();

            if (dt.Rows.Count > 0)
            {
                order.Id = Convert.ToInt32(dt.Rows[0]["ORD_ID"]);
                order.OrderStatusId = Convert.ToInt32(dt.Rows[0]["OST_ID"]);
                order.StaffId = Convert.ToInt32(dt.Rows[0]["STF_ID"]);
                order.AccountId = Convert.ToInt32(dt.Rows[0]["ACC_ID"]);
                order.Date = Convert.ToDateTime(dt.Rows[0]["ORD_DATE"]);
                order.Desc = dt.Rows[0]["ORD_DESC"].ToString();
            }
            return order;
        }

        public List<Order> getAll() {
            DataTable dt = dao.getAll();
            List<Order> list = new List<Order>();

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Order order = new Order();
                    order.Id = Convert.ToInt32(dr["ORD_ID"]);
                    order.OrderStatusId = Convert.ToInt32(dr["OST_ID"]);
                    order.StaffId = Convert.ToInt32(dr["STF_ID"]);
                    order.AccountId = Convert.ToInt32(dr["ACC_ID"]);
                    order.Date = Convert.ToDateTime(dr["ORD_DATE"]);
                    order.Desc = dr["ORD_DESC"].ToString();

                    list.Add(order);
                }

            }
            return list;
        }

        public List<OrderLine> getAllOrderLinesByAccountIdAndServiceId(int customerId, int serviceId)
        {
            DataTable dt = dao.getAllOrderLinesByAccountIdAndServiceId(customerId, serviceId);
            List<OrderLine> list = new List<OrderLine>();

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    OrderLine orderLine = new OrderLine();
                    orderLine.Id = Convert.ToInt32(dr["OLN_ID"]);
                    orderLine.OrderId = Convert.ToInt32(dr["ORD_ID"]);
                    orderLine.ServiceId = Convert.ToInt32(dr["SVC_ID"]);
                    orderLine.Quantity = Convert.ToInt32(dr["OLN_QUANTITY"]);
                    orderLine.Price = Convert.ToDouble(dr["OLN_PRICE"]);
                    orderLine.Desc = dr["SVC_NAME"].ToString() +" ("+  dr["OLN_DESC"].ToString() +")";

                    list.Add(orderLine);
                }

            }
            return list;
        }
    }
}
